<template>
	<view class="content">
		<view class="page-content">
			<view class="row b-b">
				<text class="tit">联系人</text>
				<input class="input" :maxlength="10" type="text" v-model="addressData.name" placeholder="收货人姓名"
					placeholder-class="placeholder" />
			</view>
			<view class="row b-b">
				<text class="tit">手机号</text>
				<input class="input" type="number" v-model="addressData.phoneNumber" placeholder="收货人手机号码"
					placeholder-class="placeholder" />
			</view>
			<view class="row b-b" @click="showAddressRegion">
				<text class="tit">省市区</text>
				<view class="input" style="color: #303133;" v-if="addressData.province == ''">
					请选择地址
				</view>
				<view class="input" v-else>
					{{addressData.province}} {{addressData.city}} {{addressData.region}}
				</view>
				<text class="yticon icon-you"></text>
			</view>
			<view class="input">
				<w-picker mode="region" :defaultVal="defaultRegion" :hideArea="false" @confirm="onConfirm" ref="region"
					:timeout="true"></w-picker>
			</view>
			<view class="row b-b">
				<text class="tit">详细地址</text>
				<input :maxlength="100" class="input" type="text" v-model="addressData.detailAddress" placeholder="省市区及详细地址"
					placeholder-class="placeholder" />
			</view>
			<view class="row b-b">
				<text class="tit">邮政编码</text>
				<input class="input" type="text" :maxlength="6" v-model="addressData.postCode" placeholder="邮政编码"
					placeholder-class="placeholder" />
			</view>
			<view class="row b-b">
				<text class="tit">设为默认</text>
				<switch :checked="addressData.defaultStatus == '0'" color="#7EAFA5" @change="switchChange" />
			</view>
		</view>
		
		<button class="add-btn" @click="confirm">保存</button>
		<button v-if="addressData.id != null" class="del-btn" @click="del">删除</button>
	</view>
</template>

<script>
	import wPicker from "@/components/w-picker/w-picker.vue";
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				addressData: {
					id: null,
					name: '',
					phoneNumber: '',
					province: '',
					city: '',
					region: '',
					detailAddress: '',
					postCode: '',
					defaultStatus: '1',
				},
				defaultRegion: []
			}
		},
		onLoad(option) {
			if (option.id.length > 0) {
				this.addressData.id = option.id;
				this.getAddress();
			}
		},
		computed: {
			...mapState(['hasLogin', 'userInfo'])
		},
		components: {
			wPicker
		},
		methods: {
			// 根据id获取收货地址
			getAddress() {
				try {
					this.$api.request.getMemberReceiveAddress({
						id: this.addressData.id
					}, res => this.addressData = res.data);
				} catch (e) {
					console.log(e);
				}
			},
			showAddressRegion() {
				this.$refs['region'].show();
			},
			onConfirm(val) {
				this.$set(this.addressData, 'province', val.checkArr[0]);
				this.$set(this.addressData, 'city', val.checkArr[1]);
				this.$set(this.addressData, 'region', val.checkArr[2]);
			},
			switchChange(e) {
				this.addressData.defaultStatus = e.detail.value ? '0' : '1';
			},

			//地图选择地址
			chooseLocation() {
				uni.chooseLocation({
					success: (data) => {
						this.addressData.addressName = data.name;
					}
				})
			},

			//提交
			confirm() {
				let data = this.addressData;
				if (!data.name) {
					this.$api.msg('请填写收货人姓名');
					return;
				}
				if (!this.$api.util.validateMobileNo(data.phoneNumber)) {
					this.$api.msg('请输入正确的手机号码');
					return;
				}
				// if(!data.address){
				// 	this.$api.msg('请在地图选择所在位置');
				// 	return;
				// }

				if (!data.province || !data.city || !data.region) {
					this.$api.msg('请选择省市区');
					return;
				}

				if (!data.detailAddress) {
					this.$api.msg('请填写详细地址');
					return;
				}

				try {
					this.$api.request.saveMemberReceiveAddr(data, res => {
						this.$api.msg('地址保存成功');
						// uni.navigateBack();
						// setTimeout(() => getCurrentPages().pop().$vm.refresh = true, 100)

						uni.navigateBack({
							delta: 1
						})
					});
				} catch (e) {
					console.log(e);
				}
			},
			//删除地址
			del() {
				try {
					this.$api.request.deleteMemberReceiveAddr({
						id: this.addressData.id
					}, res => {
						this.$api.msg('地址删除成功');
						// uni.navigateBack();
						// setTimeout(() => getCurrentPages().pop().$vm.refresh = true, 100)

						uni.navigateBack({
							delta: 1
						})
					});
				} catch (e) {
					console.log(e);
				}
			}
		}
	}
</script>

<style lang="scss">
	.content{
		background: #F7F8FA;
		min-height: 100vh;
	}
	.page-content{
		background: #fff;
		box-shadow: 0 4rpx 20rpx 0 rgba(0,0,0,0.0400);
		border-radius: 0 0 24rpx 24rpx;
		padding: 0 24rpx;
	}
	.row {
		display: flex;
		align-items: center;
		justify-content: space-between;
		position: relative;
		padding: 30rpx 0;

		.tit {
			flex-shrink: 0;
			width: 160rpx;
			font-size: 28rpx;
			color: $font-color-dark;
		}

		.input {
			flex: 1;
			font-size: 28upx;
			color: $font-color-dark;
			text-align: right;
		}

		.icon-shouhuodizhi {
			font-size: 36upx;
			color: $font-color-light;
		}
		.icon-you{
			margin-right: -10rpx;
			margin-top: 5rpx;
		}
	}

	.default-row {
		margin-top: 16upx;

		.tit {
			flex: 1;
		}

		switch {
			transform: translateX(16upx) scale(.9);
		}
	}

	.add-btn {
		position: fixed;
		left: 34rpx;
		right: 34rpx;
		bottom: calc(20rpx + env(safe-area-inset-bottom));
		bottom: calc(20rpx + constant(safe-area-inset-bottom));
		height: 80rpx;
		font-size: 32rpx;
		color: #fff;
		background: linear-gradient(328deg, #03D289 0%, #009B65 100%);
		border-radius: 46rpx;
		border: none;
		&::after{
			border: none;
			outline: none;
		}
	}

	.del-btn {
		position: fixed;
		left: 34rpx;
		right: 34rpx;
		bottom: calc(20rpx + env(safe-area-inset-bottom));
		bottom: calc(20rpx + constant(safe-area-inset-bottom));
		height: 80rpx;
		font-size: 32rpx;
		color: #fff;
		background-color: #dd6161;
		border-radius: 46rpx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
		&::after{
			border: none;
			outline: none;
		}
	}


	.tab {
		padding: 20upx 0;
		font-size: 32upx;
	}

	.tab.active {
		color: #f00;
	}

	.result {
		margin-top: 100upx;
		font-size: 32upx;
	}
</style>
